#include "KeyEventConvetor.h"
// #include "MCUSegDefine.h"
#include "MCUConfig.h"
#include "MCUIO.h"

// ��ʼ�����ֵ���Է���ѭ�������ҳ��黰ǰ5s���������Լ�ʹ��
#define SYSCLKINIT 5000

xdata unsigned long LastKey = 0, CurKey = 0;
// bool isDoorOpen = false;
// bool PreisDoorOpen = false;
extern xdata u16 TouchKeepCnt;
extern bool FirstFiveSecond;

// u8 KeyToFun(u32 KeyValue)
// {
//     return FUN_NONE;
// }

// u32 GetTouchValue()
// {
//     FEED_WDT();
//     HALGetTouchValue();
// }

u32 PreFilterKey = 0;
u32 CurFilterKey = 0;
u32 FKey = 0;
u8 FilterKeyCnt = 0;
#define FILTERCNT 50
u32 GetFilterKey()
{
    #if 0

    ll_wdt_feed(WDT);

#if (1 == USE_TK_JLINK_DEBUG)
    return hal_tk_key_map_get(TK);
#else
    tk_sw_alg_excute();
    CurFilterKey = tk_sw_alg_key_map_get(TK);

    #endif
    #else

    FEED_WDT();
    CurFilterKey = HAL_TouchGetValue();
    if (CurFilterKey != PreFilterKey)
    {
        FilterKeyCnt = FILTERCNT;
    }
    if (0 == FilterKeyCnt)
    {
        FKey = CurFilterKey;
    }
    PreFilterKey = CurFilterKey;
    return FKey;
#endif
}

#define KEEP_TIME 1000
u32 LastPowerKeyCnt = 0;
#if 1
u32 KeyEventConvetor()
{
    // FEED_WDT();
    // tk_sw_alg_excute();
    // CurKey = tk_sw_alg_key_map_get(TK);
    CurKey = GetFilterKey();
    switch (CurKey)
    {
    case BT_NONE:
        LastPowerKeyCnt = GetSysTick();
        return KEY_NONE;
    case BT_AIRFRYER:
        return CurKey;
    case BT_ROAST:
        return CurKey;
    case BT_BROIL:
        return CurKey;
    case BT_BAKE:
        return CurKey;
    case BT_PREHEAT:
        return CurKey;
    case BT_KEEPWARM:
        return CurKey;
    case BT_PIZZA:
        return CurKey;
    case BT_DEHYDRATE:
        return CurKey;
    case BT_TEMPADD:
        break;
    case BT_L:
        break;
    case BT_R:
        break;
    case BT_TIMEADD:
        break;
    case BT_TEMPSUB:
        break;
    case (BT_TIMEADD | BT_TEMPADD):
        if (SysTickCnt < 5000)
        {
            if ((GetSysTick() - LastPowerKeyCnt) >= (3 * KEEP_TIME))
            {
                return BT_VERSION;
            }
            else
            {
                return BT_NONE;
            }
        }
        else
        {
            return BT_NONE;
        }
    case (BT_TEMPADD | BT_TEMPSUB):
        break;
    case BT_START:
        return CurKey;
    case BT_SINCFINISH:
        return CurKey;
    case BT_SHAKE:
        return CurKey;
    case BT_DUALCLOCK:
        return CurKey;
    case BT_POWER:
        return CurKey;
    // BT_POWER_RED = 0x200,
    case BT_TIMESUB:
        break;
    case BT_PRODUCT_CHECK:
        return CurKey;
    default:
        LastPowerKeyCnt = GetSysTick();
        return KEY_NONE;
    }

    if (BT_L == CurKey)
    {
        if ((GetSysTick() - LastPowerKeyCnt) >= (3 * KEEP_TIME))
        {
            LastKey = BT_ABORT_L;
            return LastKey;
        }
        else
        {
            LastKey = BT_L;
            return LastKey;
        }
    }

    if (BT_R == CurKey)
    {
        if ((GetSysTick() - LastPowerKeyCnt) >= (3 * KEEP_TIME))
        {
            LastKey = BT_ABORT_R;
            return LastKey;
        }
        else
        {
            LastKey = BT_R;
            return LastKey;
        }
    }

#if ((1 == SOUTHAFRICA_VERSION) || (1 == SOUTHAFRICAUL_VERSION))
    if ((BT_TEMPADD | BT_TEMPSUB) == CurKey)
    {
        if ((GetSysTick() - LastPowerKeyCnt) >= (3 * KEEP_TIME))
        {
            LastKey = BT_CHANGE_TEMP_UNIT;
            return LastKey;
        }
        else
        {
            LastKey = BT_NONE;
            return LastKey;
        }
    }
#endif

    if (BT_TEMPADD == CurKey)
    {
        // if (BT_NONE == LastKey)
        // {
        //     LastPowerKeyCnt = GetSysTick();
        // }
        if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
        {
            if (((GetSysTick()) % 200) < 50)
            {
                LastKey = CurKey;
                return BT_TEMP_CONTINUE_ADD;
            }
            else
            {
                LastKey = CurKey;
                return BT_NONE;
            }
        }
        else
        {
            LastKey = CurKey;
            return BT_TEMPADD;
        }
    }

    if (BT_TEMPSUB == CurKey)
    {
        // if (BT_NONE == LastKey)
        // {
        //     LastPowerKeyCnt = GetSysTick();
        // }
        if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
        {
            if (((GetSysTick()) % 200) < 100)
            {
                LastKey = CurKey;
                return BT_TEMP_CONTINUE_SUB;
            }
            else
            {
                LastKey = CurKey;
                return BT_NONE;
            }
        }
        else
        {
            LastKey = CurKey;
            return BT_TEMPSUB;
        }
    }

    if (BT_TIMEADD == CurKey)
    {
        if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
        {
            if ((GetSysTick() % 200) < 100)
            {
                LastKey = CurKey;
                return BT_TIME_CONTINUE_ADD;
            }
            else
            {
                LastKey = CurKey;
                return BT_NONE;
            }
        }
        else
        {
            LastKey = CurKey;
            return BT_TIMEADD;
        }
    }

    if (BT_TIMESUB == CurKey)
    {
        if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
        {
            if ((GetSysTick() % 200) < 100)
            {
                LastKey = CurKey;
                return BT_TIME_CONTINUE_SUB;
            }
            else
            {
                LastKey = CurKey;
                return BT_NONE;
            }
        }
        else
        {
            LastKey = CurKey;
            return BT_TIMESUB;
        }
    }

    return KEY_NONE;
}
#else

u32 KeyEventConvetor()
{

    CurKey = GetTouchValue();
    static u32 LastPowerKeyCnt = 0; // GetSysTick();
    static u32 LastKey = 0;         //, PreKeyEvent = 0;
    if (FirstFiveSecond == true && (GetSysTick() > 5000))
    {
        FirstFiveSecond = false;
    }

    if (BT_PRODUCT_CHECK == CurKey)
    {
        if (FirstFiveSecond)
        {
            LastKey = CurKey;
            return BT_PRODUCT_CHECK;
        }
        else
        {
            LastKey = CurKey;
            return BT_NONE;
        }
    }

    // if(CurKey != BT_L || CurKey != BT_R || CurKey != BT_TEMPADD)

    {
        if ((LastKey != CurKey) && (BT_L == LastKey) && (BT_L == CurKey) && ((GetSysTick() - LastPowerKeyCnt) < 1000))
        {
            LastKey = CurKey;
            LastKey = BT_ABORT_L;
            return BT_ABORT_L;
        }
        if ((LastKey != CurKey))
        {
            if (BT_NONE != CurKey)
            {
                LastKey = CurKey;
                LastPowerKeyCnt = GetSysTick();
            }
            LastKey = CurKey;
            return CurKey;
        }

        if ((LastKey != CurKey) && (BT_R == LastKey) && (BT_R == CurKey) && ((GetSysTick() - LastPowerKeyCnt) < 1000))
        {
            LastKey = CurKey;
            LastKey = BT_ABORT_R;
            return BT_ABORT_R;
        }
        if ((LastKey != CurKey))
        {
            if (BT_NONE != CurKey)
            {
                LastKey = CurKey;
                LastPowerKeyCnt = GetSysTick();
            }
            LastKey = CurKey;
            return CurKey;
        }

        if (BT_TEMPADD == CurKey)
        {
            if (BT_NONE == LastKey)
            {
                LastPowerKeyCnt = GetSysTick();
            }
            if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
            {
                if (((GetSysTick() - LastPowerKeyCnt) % 200) < 50)
                {
                    LastKey = CurKey;
                    return BT_TEMP_CONTINUE_ADD;
                }
                else
                {
                    LastKey = CurKey;
                    return BT_NONE;
                }
            }
            else
            {

                // if (BT_NONE != CurKey && LastKey != CurKey)
                //	{
                //		switchBuzOn(SHORT_BUZ_TIME, 1);
                //	}
                switchBuzOn(SHORT_BUZ_TIME, 1);
                LastKey = CurKey;
                return BT_TEMPADD;
            }
        }

        // �¶�-�¼�ת��
        if (BT_TEMPSUB == CurKey)
        {
            if (BT_NONE == LastKey)
            {
                LastPowerKeyCnt = GetSysTick();
            }
            if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
            {
                if (((GetSysTick() - LastPowerKeyCnt) % 200) < 100)
                {
                    LastKey = CurKey;
                    return BT_TEMP_CONTINUE_SUB;
                }
                else
                {
                    LastKey = CurKey;
                    return BT_NONE;
                }
            }
            else
            {
                switchBuzOn(SHORT_BUZ_TIME, 1);
                LastKey = CurKey;
                return BT_TEMPSUB;
            }
        }

        // ʱ��+�¼�ת��
        if (BT_TIMEADD == CurKey)
        {
            if (BT_NONE == LastKey)
            {
                LastPowerKeyCnt = GetSysTick();
            }
            if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
            {
                if (((GetSysTick() - LastPowerKeyCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TIME_CONTINUE_ADD;
                }
                else
                {
                    LastKey = CurKey;
                    return BT_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TIMEADD;
            }
        }

        if (BT_TIMESUB == CurKey)
        {
            if (BT_NONE == LastKey)
            {
                LastPowerKeyCnt = GetSysTick();
            }
            if ((GetSysTick() - LastPowerKeyCnt) >= KEEP_TIME)
            {
                if (((GetSysTick() - LastPowerKeyCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TIME_CONTINUE_SUB;
                }
                else
                {
                    LastKey = CurKey;
                    return BT_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TIMESUB;
            }
        }

        LastKey = CurKey;
        return BT_NONE;

#if 0

    BF6612HTXXA1_XXXX_Set_Touch_Flag();
    switch (keys_flag)
    {
    case 0x800:
        CurKey = BT_TEMPSUB;
        break;
        //  return KEY1;
        //   break;
    case 0x400:
        CurKey = KEY2;
        return KEY2;
        //   break;
    case 0x200:
        CurKey = KEY3;
        return KEY3;
        //   break;
    case 0x80:
        CurKey = BT_TIMESUB;
        break;
    //return KEY4;
    //break;
    case 0x880:
        if (FirstFiveSecond)
        {
            return KEY_PRODUCT_CHECK;
        }
        else
        {
            return 0x0;
        }
        break;
    case 0x10:
        CurKey = BT_TIMEADD;
        break;
        // return KEY5;
    case 0x100:
        CurKey = BT_TEMPADD;
        break;
    case 0x110:
        if (FirstFiveSecond)
        {
            return KEY_CUSTOM_CHECK;
        }
        else
        {
            return 0x0;
        }
        break;
    //    return KEY6;
    default:
        CurKey = 0x0;
        break;
    }


        //Key1 Key13  Key2 Key14也需要重载，分别是调温度和事件的
        if (BT_TIMEADD == CurKey)
        {
            if (TouchKeepCnt < (KEEP_TIME - 1000))
            {
                if (((MsCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TIMEADD_CONTINUE;
                }
                else
                {
                    LastKey = CurKey;
                    return KEY_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TIMEADD;
            }
        }

        if (BT_TIMESUB == CurKey)
        {
            if (TouchKeepCnt < (KEEP_TIME - 1000))
            {
                if (((MsCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TIMESUB_CONTINUE;
                }
                else
                {
                    LastKey = CurKey;
                    return KEY_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TIMESUB;
            }
        }

        if (BT_TEMPSUB == CurKey)
        {
            if (TouchKeepCnt < (KEEP_TIME - 1000))
            {
                if (((MsCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TEMPSUB_CONTINUE;
                }
                else
                {
                    LastKey = CurKey;
                    return KEY_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TEMPSUB;
            }
        }

        //时间+事件转化
        if (BT_TEMPADD == CurKey)
        {
            if (TouchKeepCnt < (KEEP_TIME - 1000))
            {
                if (((MsCnt) % 100) < 50)
                {
                    LastKey = CurKey;
                    return BT_TEMPADD_CONTINUE;
                }
                else
                {
                    LastKey = CurKey;
                    return KEY_NONE;
                }
            }
            else
            {
                LastKey = CurKey;
                return BT_TEMPADD;
            }
        }

        if (KEY_NONE == CurKey)
        {
            TouchKeepCnt = KEEP_TIME;
        }
        LastKey = CurKey;
        return CurKey;
#endif
    }
}

#endif
